package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.dto.RegionDTO;
import com.heima.pojo.vo.RegionVO;
import com.heima.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    RegionMapper regionMapper;

    public void countNode(){
        regionMapper.countNode();
    }

    @Override
    public PageResult<RegionVO> page(RegionDTO regionDTO) {
        //用pageHelper完成分页查询
        //1.设置分页参数
        PageHelper.startPage(regionDTO.getPageIndex(), regionDTO.getPageSize());
        //2.查询数据 - 先更新区域的点数总和
        countNode();
        List<RegionVO> regionList = regionMapper.page(regionDTO);
        //3.封装数据
        Page page = ( Page) regionList;
        PageResult<RegionVO> pageResult = new PageResult<>();
        pageResult.setPageIndex(regionDTO.getPageIndex());
        pageResult.setPageSize(regionDTO.getPageSize());
        pageResult.setTotalCount(page.getTotal());
        pageResult.setTotalPage(page.getPages());
        pageResult.setCurrentPageRecords(page.getResult());
        return pageResult;
    }
}
